# Copyright (C) 2009,2010,2011,2012  Marco Restelli
#
# This file is part of:
#   FEMilaro -- Finite Element Method toolkit
#
# FEMilaro is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# FEMilaro is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FEMilaro; If not, see <http://www.gnu.org/licenses/>.
#
# author: Marco Restelli                   <marco.restelli@gmail.com>

# List of the object files of the present folder
OBJ= mod_time_integrators_base.o \
     mod_rk.o \
     mod_multistep.o \
     mod_expo.o \
     mod_spexpo.o \
     mod_time_integrators.o
# These are the modules that are meant to be USEd by the library users
LIBRARY_MOD_FILES= mod_time_integrators.mod
OBJ_ODETEST= ode_test.o \
     $(OBJ)

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(BUILDDIR)/time-integrators

LIBS:=-lfml_general_utilities $(LIBS)
LDFLAGS:=$(LDFLAGS) -L$(LIBDIR)

# VPATH includes:
#   PWD for the source files (included by default)
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the libfml_general_utilities.a library
#   BINDIR for the final executable
VPATH:=$(BUILDDIR) $(LIBDIR) $(BINDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# Main targets ---------------------------------------

libode.a: $(OBJ)
	$(RM) $(LIBDIR)/libode.a
	cd $(BUILDDIR) && $(AR) -r $(LIBDIR)/libode.a $(OBJ)
	for mod in $(LIBRARY_MOD_FILES);     \
        do                                   \
          cp $(BUILDDIR)/$${mod} $(LIBDIR)/; \
        done

ode-test: $(OBJ_ODETEST)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_ODETEST) $(LIBS) -o $(BINDIR)/ode-test

# General rule: all the .o depend on the libfml_general_utilities library
%.o: %.f90 libfml_general_utilities.a
	cd $(BUILDDIR) && \
	  $(FC) $(FFLAGS) $(MODFLAG)$(LIBDIR) -c $(PWD)/$<

clean:
	$(RM) $(BUILDDIR)/*.o $(BUILDDIR)/*.ilo $(BUILDDIR)/*.mod \
	  $(LIBDIR)/libode.a $(BINDIR)/ode-test
	for mod in $(LIBRARY_MOD_FILES); \
        do                               \
          $(RM) $(LIBDIR)/$${mod};       \
        done

# Specific dependencies: because of USE directives ---
# Notice: here we list only the prerequisites which are not already
# included in the libfml_general_utilities.a library, which is a
# prerequisite of all the .o files listed in this Makefile

ode_test.o: \
   mod_time_integrators.o

mod_time_integrators.o: \
   mod_time_integrators_base.o \
   mod_rk.o \
   mod_multistep.o \
   mod_expo.o \
   mod_spexpo.o

mod_rk.o: \
   mod_time_integrators_base.o

mod_multistep.o: \
   mod_time_integrators_base.o

mod_expo.o: \
   mod_time_integrators_base.o

mod_spexpo.o: \
   mod_time_integrators_base.o \
   mod_expo.o

